set more off
global user `c(username)'
*** Set WD
cd "/Users//$user/Dropbox/SurveyCovid19/"


*** Directories
global data "C:\Users\\$user\Dropbox\SurveyCovid19\Descriptive paper\Data for replication"
global tables "Descriptive paper\Tables\"
global figures "Descriptive paper\Figures\"
global enddate"1may2020"

use "${data}\data_Table1.dta", clear
/*

gen year=substr(recordeddate,1,4)
destring year, replace
gen month=substr(recordeddate,6,2)
destring month, replace
gen day=substr(recordeddate,9,2)
destring day, replace
gen date_string=substr(recordeddate,1,10)
g first_answer=mdy(3,27,2020)
g date=mdy(month,day,year)
format date %td




foreach x in redujo_ingreso pol_prioridad ingreso_antes ingreso_ahora trabajocasa darprestamos recibeprestamos  perdioempleo cerronegocio_dem cerronegocio_gob  menos_saludable {
replace `x'=. if `x'<0
sum `x'
}



foreach x in     pol_prioridad   menos_saludable {
g h_`x'=`x'>3
replace h_`x'=. if `x'==.
}
label var h_pol_prioridad "Gov priority should be COVID19"
g h_tiempo_cerrar=tiempo_negocios_cerrar>=3
replace h_tiempo_cerrar=. if tiempo_negocios_cerrar==-99 | tiempo_negocios_cerrar==.
*Note: Tiempo negocios cerrar =-98 refers to people that do not think that businesses should close at all, therefore is coded as 0.
label var h_tiempo_cerrar "Agrees with closing non-essential biz for a month or more"
label var h_menos_saludable "Agrees eating less healthier"


replace ingreso_antes=9 if ingreso_antes==10
replace ingreso_ahora=9 if ingreso_ahora==10

label define ingres_antes 1 "0-0.5" 2 "0.5-1" 3 "1-2" 4 "2-3" 5 "3-4" 6 "4-6" 7 "6-8" 8 "8-11" 9 "11>" 
label values ingreso_antes ingres_antes
label values ingreso_ahora ingres_antes




g cerronegocio=(cerronegocio_dem==1 | cerronegocio_gob==1)
replace cerronegocio=. if cerronegocio_dem==. & cerronegocio_gob==.

g lost_livelihood=perdioempleo==1 | cerronegocio==1

** Locality X time fixed effects
egen locfeXd=group(localidad_cod countryip date)
egen locfe=group(localidad_cod countryip )
egen countryXdate=group(date countryip)
egen countryFe=group(countryip)
label var locfeXd "Locality-date FE" 

rename countryip country
joinby country using "Descriptive paper/Other Data/datos_corona_country.dta", unm(m)
drop _merge

foreach x in  genero edad hayninhos nrhogar_antes haymayores {
replace `x'=.  if `x'<0
}

gen income_reduction=redujo_ingreso
replace income_reduction=. if (redujo_ingreso==. | redujo_ingreso==.)


g female=genero==1



*/
global dem "female edad hayninhos nrhogar_antes haymayores i.educ"



***** Panel A: Impacts of job loss during the crisis: Includes locality-date FE
foreach x in  income_reduction hambre h_menos_sa recibeprestamo h_pol_prio h_tiempo_cerrar {
reghdfe `x' lost_livelihood  $dem [w=ipw_c], a(locfeXd sector) cl(locfe)
eststo `x' 
}

**** Panel B: Impacts by informality
foreach x in  income_reduction hambre h_menos_sa recibeprestamo h_pol_prio h_tiempo_cerrar {
reghdfe `x' c.lost_livelihood#c.selfemployed lost_livelihood  $dem [w=ipw_c], a(locfeXd sector) cl(locfe)
eststo se_`x' 
reghdfe `x' c.lost_livelihood#c.informal lost_livelihood  $dem [w=ipw_c], a(locfeXd sector) cl(locfe)
eststo inf_`x' 
}



label var lost_livelihood "Lost job or closed business"
label var income_reduction "Decreased income"
label var hambre "Went hungry"
label var h_menos_sa "Less healthy"
label var recibeprestamo "Gift/Loan"
label var h_pol_pri "Gov. priority"
label var h_tiempo_c "Lockdown (>=month)"

label var female "Female"
cd "/Users//$user/Dropbox/SurveyCovid19/Descriptive paper/Tables/" 

esttab income_reduction hambre h_menos_sa recibeprestamo h_pol_prio h_tiempo_cerrar using "Table 1.csv", se keep(lost_livelihood) replace  mtitles("Decreased income" "Went hungry" "Eats less healthy" "Gift/Loan" "Gov. Priority" "Lockdown (>=month)") ar2  nodepvars nogaps compress label star(* .1 ** .05 *** .01) b(%8.3f) se(%8.3f)
esttab se_income_reduction se_hambre se_h_menos_sa se_recibeprestamo se_h_pol_prio se_h_tiempo_cerrar using "Table 1.csv", se keep(lost_livelihood c.lost_livelihood#c.selfemployed) append  mtitles("Decreased income" "Went hungry" "Eats less healthy" "Gift/Loan" "Gov. Priority" "Lockdown (>=month)") ar2  nodepvars nogaps compress label star(* .1 ** .05 *** .01) b(%8.3f) se(%8.3f)
esttab inf_income_reduction inf_hambre inf_h_menos_sa inf_recibeprestamo inf_h_pol_prio inf_h_tiempo_cerrar using "Table 1.csv", se keep(lost_livelihood c.lost_livelihood#c.informal) append  mtitles("Decreased income" "Went hungry" "Eats less healthy" "Gift/Loan" "Gov. Priority" "Lockdown (>=month)") ar2  nodepvars nogaps compress label star(* .1 ** .05 *** .01) b(%8.3f) se(%8.3f)


*** Robustness without weights:


foreach x in  income_reduction hambre h_menos_sa recibeprestamo h_pol_prio h_tiempo_cerrar {
reghdfe `x' lost_livelihood  $dem , a(locfeXd sector) cl(locfe)
eststo `x'_2 
}

esttab income_reduction_2 hambre_2 h_menos_sa_2 recibeprestamo_2 h_pol_prio_2 h_tiempo_cerrar_2 using "Table Robustness.csv", se keep(lost_livelihood ) replace  ar2 mtitles  nogaps compress label star(* .1 ** .05 *** .01) b(%8.3f) se(%8.3f) mtitles("Decreased income" "Went hungry" "Eats less healthy" "Gift/Loan" "Gov. Priority" "Lockdown (>=month)")



*** Robustness with countr-date FE


foreach x in  income_reduction hambre h_menos_sa recibeprestamo h_pol_prio h_tiempo_cerrar {
reghdfe `x' lost_livelihood  $dem [w=ipw_c], a(countryXdate sector) cl(countryFe)
eststo `x'_2 
}

esttab income_reduction_2 hambre_2 h_menos_sa_2 recibeprestamo_2 h_pol_prio_2 h_tiempo_cerrar_2 using "Table Robustness.csv", se keep(lost_livelihood )  append ar2 mtitles  nogaps compress label star(* .1 ** .05 *** .01) b(%8.3f) se(%8.3f) mtitles("Decreased income" "Went hungry" "Eats less healthy" "Gift/Loan" "Gov. Priority" "Lockdown (>=month)")




foreach x in  income_reduction hambre h_menos_sa recibeprestamo h_pol_prio h_tiempo_cerrar {
reghdfe `x' lost_livelihood  $dem , a(countryXdate sector) cl(countryFe)
eststo `x'_2 
}

esttab income_reduction_2 hambre_2 h_menos_sa_2 recibeprestamo_2 h_pol_prio_2 h_tiempo_cerrar_2 using "Table Robustness.csv", se keep(lost_livelihood )  append ar2 mtitles  nogaps compress label star(* .1 ** .05 *** .01) b(%8.3f) se(%8.3f) mtitles("Decreased income" "Went hungry" "Eats less healthy" "Gift/Loan" "Gov. Priority" "Lockdown (>=month)")
